home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / utility / umult32.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  2KB  |  93 lines

  1. /*
  2.     $Id: umult32.c,v 1.1 1996/08/31 12:58:13 aros Exp $
  3.     $Log: umult32.c,v $
  4.     Revision 1.1  1996/08/31 12:58:13  aros
  5.     Merged in/modified for FreeBSD.
  6.  
  7.     Desc:
  8.     Lang: english
  9. */
  10. #include "utility_intern.h"
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15.         #include <clib/utility_protos.h>
  16.  
  17.         __AROS_LH2(ULONG, UMult32,
  18.  
  19. /*  SYNOPSIS */
  20.         __AROS_LHA(unsigned long, arg1, D0),
  21.         __AROS_LHA(unsigned long, arg2, D1),
  22.  
  23. /*  LOCATION */
  24.         struct UtilityBase *, UtilityBase, 24, Utility)
  25.  
  26. /*  FUNCTION
  27.         Performs an unsigned 32-bit multiplication of arg1 * arg2 and
  28.         returns a 32 bit value.
  29.  
  30.     INPUTS
  31.         arg1, arg2  -   32 bit unsigned longs
  32.  
  33.     RESULT
  34.         arg1 * arg2
  35.  
  36.     NOTES
  37.         This can perform the multiplication either using the machines
  38.         native instructions (if they exist), or in software using a
  39.         simple algorithm (three multiplications, two shifts and
  40.         an addition.
  41.  
  42.     EXAMPLE
  43.  
  44.         long a = 352543;
  45.         long b = 52464;
  46.         long c = UMult32(a,b);
  47.         c == 1315946768
  48.  
  49.     BUGS
  50.  
  51.     SEE ALSO
  52.         utility/SMult32(), utility/UMult64(), utility/SMult64()
  53.  
  54.     INTERNALS
  55.         We are performing the operation:
  56.  
  57.  
  58.             (2^16 * a + b) * (2^16 * c + d)
  59.           = 2^32 * ab + 2^16 * ad + 2^16 * bc + bd
  60.           = 2^32 * ab + 2^16 ( ad + bc ) + bd
  61.  
  62.         Now since the result is a 32-bit number, the 2^32 term will have
  63.         no effect. (Since 2^32 > max (32-bit number).
  64.  
  65.         Therefore:
  66.         product = 2^16( ad + bc ) + bd
  67.  
  68.     HISTORY
  69.         29-10-95    digulla automatically created from
  70.                             utility_lib.fd and clib/utility_protos.h
  71.         18-08-96    iaint   Implemented as described above.
  72.  
  73. *****************************************************************************/
  74. {
  75.     __AROS_FUNC_INIT
  76.  
  77. #ifdef HAS_32BITMULU
  78.     return arg1 * arg2;
  79. #else
  80.  
  81.     UWORD a0, a1, b0, b1;
  82.  
  83.     a1 = (arg1 >> 16) & 0xffff;
  84.     a0 = arg1 & 0xffff;
  85.     b1 = (arg2 >> 16) & 0xffff;
  86.     b0 = arg2 & 0xffff;
  87.  
  88.     return (((a0 * b1) + (a1 * b0)) << 16) + (b0 * a0);
  89. #endif
  90.  
  91.     __AROS_FUNC_EXIT
  92. } /* UMult32 */
  93.